﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFrom
{
    public partial class Form2 : Form
    {
        public Form1 Form1 = null;
        public Form2(Form1 form1)
        {
            InitializeComponent();
            Form1 = form1;
        }

        public static bool bl = false;

        public static string ImageTypeName = "";

        public static string zooming = "";

        public static string FileType = "";

        private void button1_Click(object sender, EventArgs e)
        {
            if (FileType == "")
            {
                MessageBox.Show("请选择文件类型");
                return;
            }
            if (zooming == "")
            {
                MessageBox.Show("请选择图片缩放格式");
                return;
            }
            if (textBox2.Text == "")
            {
                MessageBox.Show("请选择保存路径");
                return;
            }
            if (ImageTypeName == "")
            {
                MessageBox.Show("请选择保存格式");
                return;
            }
            if (FileType == "目录" && textBox5.Text == "")
            {
                MessageBox.Show("请选择压缩目录");
                return;
            }
            if (FileType == "文件" && textBox1.Text == "")
            {
                MessageBox.Show("请选择压缩文件");
                return;
            }
            if (textBox3.Text == "" || textBox4.Text == "")
            {
                MessageBox.Show("缩放比列不能为空");
                return;
            }
            //if (!File.Exists(textBox1.Text))
            //{
            //    MessageBox.Show("文件为空");
            //    return;
            //}
            if (FileType == "文件" && !Directory.Exists(textBox2.Text))
            {
                MessageBox.Show("不存在此文件夹");
                return;
            }
            if (FileType == "目录" && !Directory.Exists(textBox5.Text))
            {
                MessageBox.Show("不存在此文件目录");
                return;
            }
            var bl = true;
            if (FileType == "文件")
            {
                var savPath = Path.Combine(textBox2.Text, Guid.NewGuid().ToString()) + ImageTypeName;
                bl = ImageTool.ImageTool.CompressPercent(textBox1.Text, savPath, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), zooming);
            }
            else
            {
                bl = ImageTool.ImageTool.CompressPercentDictionNary(textBox5.Text, Convert.ToInt32(textBox3.Text), Convert.ToInt32(textBox4.Text), zooming, textBox2.Text);
            }

            if (bl == true)
            {
                MessageBox.Show("成功");
            }
            else
            {
                MessageBox.Show("失败");
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1.Visible = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("退出成功");
            System.Environment.Exit(0);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //文件夹对话框
            openFileDialog1.ShowDialog();
            openFileDialog1.Title = "请选择文件";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var wewe = openFileDialog1.FileName;
                var fileExtend = Path.GetExtension(wewe);
                var i = fileExtend.LastIndexOf(".");
                string StrType = fileExtend.Substring(i);
                if (StrType == ".jpg" || StrType == ".gif" || StrType == ".jpeg" || StrType == ".png")
                {
                    bl = true;
                    textBox1.Text = wewe;
                    pictureBox1.BackgroundImage = Image.FromFile(textBox1.Text);
                    pictureBox1.BackgroundImageLayout = ImageLayout.Stretch;
                }
                else
                {
                    MessageBox.Show("文件格式有误");
                    bl = false;
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void button4_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.Description = "请选择文件路径";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                string foldPath = folderBrowserDialog1.SelectedPath;
                textBox2.Text = foldPath;
                //MessageBox.Show("");

            }
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            ImageTypeName = ".jpg";
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            ImageTypeName = ".jpeg";
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            ImageTypeName = ".png";
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
        {

        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            zooming = "缩小";
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            zooming = "放大";
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void radioButton7_CheckedChanged(object sender, EventArgs e)
        {
            FileType = "目录";
        }

        private void radioButton6_CheckedChanged(object sender, EventArgs e)
        {
            FileType = "文件";
        }

        private void panel3_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            folderBrowserDialog2.Description = "请选压缩目录";
            if (folderBrowserDialog2.ShowDialog() == DialogResult.OK)
            {
                string foldPath = folderBrowserDialog2.SelectedPath;
                textBox5.Text = foldPath;
                //MessageBox.Show("");

            }
        }
    }
}
